package com.shcz.migu.ui.base;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;

import com.shcz.migu.api.APIService;


/**
 * Created by yfjin on 15/10/29.
 */
abstract public class ShakeActivity extends AppCompatActivity implements SensorEventListener {


    SensorManager sensorManager = null;
    boolean isDebug = APIService.isDebug;

    @Override
    public void setContentView(int layoutResID) {

        super.setContentView(layoutResID);
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {

        if (isDebug) {
            int sensorType = event.sensor.getType();
            float[] values = event.values;
            if (sensorType == Sensor.TYPE_ACCELEROMETER) {
                if ((Math.abs(values[0]) > 17 || Math.abs(values[1]) > 17 || Math
                        .abs(values[2]) > 17)) {
//                    Intent intent = new Intent(this, DebugActivity.class);
//                    startActivity(intent);
                }

            }
        }


    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }


    @Override
    protected void onPause() {
        super.onPause();

        if (isDebug) {
            sensorManager.unregisterListener(this);
        }


    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isDebug) {
            sensorManager.registerListener(this,
                    sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                    SensorManager.SENSOR_DELAY_NORMAL);
        }

    }
}
